package com.acme.bankapp.service.bank;

import com.acme.bankapp.domain.bank.AbstractAccount;
import com.acme.bankapp.domain.bank.Bank;
import com.acme.bankapp.domain.bank.Client;
import com.acme.bankapp.domain.bank.exceptions.ClientExistsException;

import java.io.*;

/**
 * Created by anton on 8/18/14.
 */
public class BankService {
    public static void addClient(Bank bank, Client client) throws ClientExistsException {
        try {
            bank.addClient(client);
        } catch (ClientExistsException e) {
            throw e;
        }
    }
    public static void printMaximumAmountToWithdraw(Bank bank) {
        for (Client client : bank.getClients()) {
            AbstractAccount acc = client.getAccount();
            System.out.println(acc.maximumAmountToWithdraw());
        }
    }

    public static void saveBank(Bank bank) {
        try(ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream(new File("/Users/anton/Developer/javaSchool/feed/bank.ser")))) {
            oos.writeObject(bank);
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public static Bank readBank() {
        Bank bank = null;
        try(ObjectInputStream ois = new ObjectInputStream(new FileInputStream(new File("/Users/anton/Developer/javaSchool/feed/bank.ser")))) {
            bank = (Bank) ois.readObject();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        }
        return bank;
    }
}
